<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式典型应用之模态窗口</title>
</head>
<body>
    <button class="btn">打开</button>
    <button class="btn">关闭</button>
    <script>
        class LoginModule {
            static _login_module;
            constructor(){
               this.loginElement = document.createElement('div');
                this.loginElement.innerHTML=`
                    <form>
                        <input type='text' placeholder='请输入您的账号'/>
                        <input type='text' placeholder='请输入您的密码'/>
                        <button>登陆</button>
                    </form>
                `;
                this.loginElement.style.cssText='width:400px;height:200px;border-radius:10px;border:1px solid red;position:absolute;left:50%;top:50%';
                document.body.appendChild(this.loginElement);
            };
            show(){
                this.loginElement.style.display='block';
            };
            hide(){
                this.loginElement.style.display='none';
            };
            static createLogin(){
                if(!LoginModule._login_module){
                    // console.log('没有走缓存');
                    LoginModule._login_module = new LoginModule();
                }
                // console.log('走缓存');
                document.querySelectorAll('.btn')[0].addEventListener('click',_=>{
                    LoginModule.createLogin().show();
                });
                document.querySelectorAll('.btn')[1].addEventListener('click',_=>{
                    LoginModule.createLogin().hide();
                })
                return LoginModule._login_module
               
            }

            
        };
        
        LoginModule.createLogin()
    </script>
    
</body>
</html>